﻿#ifndef WRWORKTCPCOMMSERVERDATASOURCE
#define WRWORKTCPCOMMSERVERDATASOURCE

#include "WRWorkTcpCommServerInterface.h"
#include "WRWorkTcpCommServerInterfaceActivator.h"
#include "../WRWorkTcpComm.h"
#include "../WRWorkTcpCommConsts.h"
#include "../../WRSoft-Utility/Structs/WRBuffers.h"
#include "../../WRSoft-Utility/Framing/WRDataSource.h"
#include "../../WRSoft-Utility/Runtime/WRDataPacket.h"

#include <QQueue>

/********************
class TArgsWriter
{
public:
    int count(const T& val);
    int write(const T& val, unsigned char* buf);
};
class TDataWriter
{
public:
    int count(const T& val);
    int write(const T& val, unsigned char* buf);
};
********************/

//表示工作TCP通讯服务器数据源接口。
template <class TArgs, class TData, class TArgsWriter, class TDataWriter>
class WRWorkTcpCommServerDataSource : public WRWorkTcpCommServerInterface, public WRDataHandler
{
private:
    WRDataSource* m_source;
    TArgsWriter m_argsWriter;
    TDataWriter m_dataWriter;
    bool m_status;
    WR32s m_bufSize;//缓存数量
    WR32s m_pkgSize;//每包大小
    WRBuffers<char>* m_dataBuffers;
    WR64s m_dataProcCounter;    //处理数据计数，即为处理的总数据量。
    WR64s m_dataSentCounter;    //发送数据计数，即为发送的总数据量。

    //表示处理项目。
    class ProcEntry : public WRDataPacket
    {
    public:
        //执行处理。
        virtual bool process(void) = 0;
    };

    QQueue<ProcEntry*> m_procQueue; //处理队列
    QMutex m_procMutex;     //处理同步
    ProcEntry* m_procEntry; //当前处理项目

    //启动处理。
    void beginProcess(ProcEntry* entry)
    {
        QMutexLocker lck(&m_procMutex);
        if (m_procEntry)
        {
            m_procQueue.enqueue(entry);
        }
        else
        {
            m_procEntry = entry;
            m_procEntry->process();
            m_procEntry->addRef();
            if (!sendData(m_procEntry))
            {
                m_status = false;
                m_procEntry->release();
                m_procEntry->release();
                m_procEntry = 0;
            }
        }
    }
    //完成处理。
    void endProcess(int length)
    {
        QMutexLocker lck(&m_procMutex);
        if (length < 0)
        {
            m_status = false;
            m_procEntry->release();
            while(m_procQueue.count())
            {
                m_procEntry = m_procQueue.dequeue();
                m_procEntry->release();
            }
            m_procEntry = 0;
        }
        else if (m_procEntry->process())
        {
            m_procEntry->addRef();
            if (!this->sendData(m_procEntry))
            {
                m_status = false;
                m_procEntry->release();
                m_procEntry->release();
                while(m_procQueue.count())
                {
                    m_procEntry = m_procQueue.dequeue();
                    m_procEntry->release();
                }
                m_procEntry = 0;
            }
        }
        else
        {
            m_procEntry->release();
            m_procEntry = 0;

            if (m_procQueue.count())
            {
                m_procEntry = m_procQueue.dequeue();
                m_procEntry->process();
                m_procEntry->addRef();
                if (!sendData(m_procEntry))
                {
                    m_status = false;
                    m_procEntry->release();
                    m_procEntry->release();
                    while(m_procQueue.count())
                    {
                        m_procEntry = m_procQueue.dequeue();
                        m_procEntry->release();
                    }
                    m_procEntry = 0;
                }
            }
        }
    }

    //表示状态处理项目，包括启动、改变和停止状态。
    class StatProcEntry : public ProcEntry
    {
    private:
        WRWorkTcpCommServerDataSource* m_owner;
        char* m_buffer;
        int m_length;
        int m_counter;

    public:
        //初始化状态处理项目。
        StatProcEntry(WRWorkTcpCommServerDataSource* owner, int length)
            : m_owner(owner), m_buffer(new char[length]), m_length(length), m_counter(0)
        {
            if (m_owner->affair())
            {
                m_owner->affair()->addRef();
            }
        }
        //清理当前对象所占用的相关资源。
        ~StatProcEntry()
        {
            delete[] m_buffer;
            if (m_owner->affair())
            {
                m_owner->affair()->release();
            }
        }

        //执行处理。
        bool process(void)
        {
            ++m_counter;
            if (m_counter > 1)
                return false;
            return true;
        }

        //获取数据缓存。
        int buffer(char** p)
        {
            *p = m_buffer;
            return m_length;
        }
        //当完成后调用此方法。
        void fulfil(int length)
        {
            m_owner->endProcess(length);
        }
    };
    //表示对象数据处理项目。
    class ObjectDataProcEntry : public ProcEntry
    {
    private:
        WRWorkTcpCommServerDataSource* m_owner;
        int m_bufStart;
        int m_datTotal;
        int m_pkgIndex;
        int m_pkgTotal;
        int m_pkgSize;
        int m_cmdStart;
        int m_cmdCount;
        WRBuffer<char>* m_buffer;
        WRBuffers<char>* m_buffers;

    public:
        //初始化对象数据处理项目。
        ObjectDataProcEntry(WRWorkTcpCommServerDataSource* owner, WRBuffer<char>* buffer, const TData& value)
            : m_owner(owner), m_buffer(buffer), m_buffers(owner->m_dataBuffers)
        {
            m_datTotal = m_owner->m_dataWriter.count(value);

            m_buffer->reset(48 + m_datTotal);
            m_owner->m_dataWriter.write(value, ((unsigned char*)(m_buffer->array())) + 32);

            m_pkgSize = m_owner->m_pkgSize;
            m_pkgIndex = 0;
            m_bufStart = 0;
            m_pkgTotal = m_datTotal / m_pkgSize;
            if (m_datTotal % m_pkgSize > 0) m_pkgTotal += 1;

            m_buffers->addRef();
        }
        //清理当前对象所占用的相关资源。
        ~ObjectDataProcEntry()
        {
            m_buffers->release(m_buffer);
            m_buffers->release();
        }

        //执行处理。
        bool process(void)
        {
            //如果已经发送完成，则释放资源
            if (m_pkgIndex == m_pkgTotal)
            {
                return false;
            }
            //包索引自增
            ++m_pkgIndex;
            //取得缓存块相关信息
            int dataLen = 0;//发送数据大小
            int fillLen = 0;//填充数据长度
            m_cmdStart = m_bufStart;
            char* buffer = (char*)m_buffer->array();
            if (m_pkgIndex > 1)//除第一包数据外，从缓存块末尾恢复初占用的尾部数据
            {
                memcpy(buffer + m_cmdStart + 32, buffer + m_buffer->count() - 8, 8);
            }
            if (m_pkgIndex < m_pkgTotal) //非结束数据包处理
            {
                //计算相应的数据长度
                dataLen = m_pkgSize;
                m_cmdCount = 40 + dataLen;
                m_bufStart += dataLen;
                //除最后一包数据外，需要备份末尾数据到缓存块末尾
                memcpy(buffer + m_buffer->count() - 8, buffer + m_cmdStart + m_cmdCount - 8, 8);
            }
            else//结束数据包处理
            {
                dataLen = m_buffer->count() - m_cmdStart - 48;
                {
                    int remain = dataLen % 8;
                    if (remain != 0) fillLen = 8 - remain;
                }
                m_cmdCount = 40 + dataLen + fillLen;
            }
            //执行封包处理
            char* p = buffer + m_cmdStart;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
            p += 4;
            WRBytes::write((WR32s)(m_cmdCount), p);//帧长度
            p += 4;
            WRBytes::write((WR16u)(m_owner->object()->index()), p);//工作对象索引
            p += 2;
            WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectIITreat, p);//类型（接口处理）
            p += 2;
            WRBytes::write((WR16u)(m_owner->index()), p);//接口索引
            p += 2;
            WRBytes::write((WR16u)WRWorkTcpComm::DataProcess, p);//数据处理
            p += 6;
            WRBytes::write((WR32s)(m_datTotal), p);//数据总长度
            p += 4;
            WRBytes::write((WR16u)(m_pkgTotal), p);//数据包数量
            p += 2;
            WRBytes::write((WR16u)(m_pkgIndex), p);//数据包索引
            p += 2;
            WRBytes::write((WR32s)(dataLen), p);//数据内容长度
            p += 4;
            p += dataLen;
            p += fillLen;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
            p += 4;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾

            return true;
        }

        //获取数据缓存。
        int buffer(char** p)
        {
            *p = ((char*)m_buffer->array()) + m_cmdStart;
            return m_cmdCount;
        }
        //当完成后调用此方法。
        void fulfil(int length)
        {
            m_owner->endProcess(length);
        }
    };
    //表示事务数据处理项目。
    class AffairDataProcEntry : public ProcEntry
    {
    private:
        WRWorkTcpCommServerDataSource* m_owner;
        int m_bufStart;
        int m_datTotal;
        int m_pkgIndex;
        int m_pkgTotal;
        int m_pkgSize;
        int m_cmdStart;
        int m_cmdCount;
        WRBuffer<char>* m_buffer;
        WRBuffers<char>* m_buffers;

    public:
        //初始化事务数据处理项目。
        AffairDataProcEntry(WRWorkTcpCommServerDataSource* owner, WRBuffer<char>* buffer, const TData& value)
            : m_owner(owner), m_buffer(buffer), m_buffers(owner->m_dataBuffers)
        {
            m_owner->affair()->addRef();

            m_datTotal = m_owner->m_dataWriter.count(value);

            m_buffer->reset(56 + m_datTotal);
            m_owner->m_dataWriter.write(value, ((unsigned char*)m_buffer->array()) + 40);

            m_pkgSize = m_owner->m_pkgSize;
            m_pkgIndex = 0;
            m_bufStart = 0;
            m_pkgTotal = m_datTotal / m_pkgSize;
            if (m_datTotal % m_pkgSize > 0) m_pkgTotal += 1;

            m_buffers->addRef();
        }
        //清理当前对象所占用的相关资源。
        ~AffairDataProcEntry()
        {
            m_buffers->release(m_buffer);
            m_buffers->release();
            m_owner->affair()->release();
        }

        //执行处理。
        bool process(void)
        {
            //如果已经发送完成，则释放资源
            if (m_pkgIndex == m_pkgTotal)
            {
                return false;
            }
            //包索引自增
            ++m_pkgIndex;
            //取得缓存块相关信息
            int dataLen = 0;//发送数据大小
            int fillLen = 0;//填充数据长度
            m_cmdStart = m_bufStart;
            char* buffer = (char*)m_buffer->array();
            if (m_pkgIndex > 1)//除第一包数据外，从缓存块末尾恢复初占用的尾部数据
            {
                memcpy(buffer + m_cmdStart + 40, buffer + m_buffer->count() - 8, 8);
            }
            if (m_pkgIndex < m_pkgTotal) //非结束数据包处理
            {
                //计算相应的数据长度
                dataLen = m_pkgSize;
                m_cmdCount = 48 + dataLen;
                m_bufStart += dataLen;
                //除最后一包数据外，需要备份末尾数据到缓存块末尾
                memcpy(buffer + m_buffer->count() - 8, buffer + m_cmdStart + m_cmdCount - 8, 8);
            }
            else//结束数据包处理
            {
                dataLen = m_buffer->count() - m_cmdStart - 56;
                {
                    int remain = dataLen % 8;
                    if (remain != 0) fillLen = 8 - remain;
                }
                m_cmdCount = 48 + dataLen + fillLen;
            }
            //执行封包处理
            char* p = buffer + m_cmdStart;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
            p += 4;
            WRBytes::write((WR32s)(m_cmdCount), p);//帧长度
            p += 4;
            WRBytes::write((WR16u)(m_owner->object()->index()), p);//工作对象索引
            p += 2;
            WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectAffair, p);//类型（工作事务）
            p += 2;
            WRBytes::write((WR32s)(m_owner->affair()->id()), p);
            p += 4;
            WRBytes::write((WR16u)(m_owner->affair()->index()), p);//工作对象索引
            p += 2;
            WRBytes::write((WR16u)WRWorkTcpComm::WorkAffairIITreat, p);//类型（接口处理）
            p += 2;
            WRBytes::write((WR16u)(m_owner->index()), p);//接口索引
            p += 2;
            WRBytes::write((WR16u)WRWorkTcpComm::DataProcess, p);//数据处理
            p += 6;
            WRBytes::write((WR32s)(m_datTotal), p);//数据总长度
            p += 4;
            WRBytes::write((WR16u)(m_pkgTotal), p);//数据包数量
            p += 2;
            WRBytes::write((WR16u)(m_pkgIndex), p);//数据包索引
            p += 2;
            WRBytes::write((WR32s)(dataLen), p);//数据内容长度
            p += 4;
            p += dataLen;
            p += fillLen;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
            p += 4;
            WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾

            return true;
        }

        //获取数据缓存。
        int buffer(char** p)
        {
            *p = ((char*)m_buffer->array()) + m_cmdStart;
            return m_cmdCount;
        }
        //当完成后调用此方法。
        void fulfil(int length)
        {
            m_owner->endProcess(length);
        }
    };

public:
    //初始化工作TCP通讯服务器 数据源接口。
    WRWorkTcpCommServerDataSource(const QUuid& uuid, const QString& name, int index, WRWorkTcpCommServerObject* target, WRDataSource* source)
        : WRWorkTcpCommServerInterface(uuid, name, index, target),
          m_source(source), m_status(true), m_dataBuffers(0), m_dataProcCounter(0), m_dataSentCounter(0), m_procEntry(0)
    {
        //add code here
    }
    //初始化工作TCP通讯服务器 数据源接口。
    WRWorkTcpCommServerDataSource(const QUuid& uuid, const QString& name, int index, WRWorkTcpCommServerAffair* target, WRDataSource* source)
        : WRWorkTcpCommServerInterface(uuid, name, index, target),
        m_source(source), m_status(true), m_dataBuffers(0), m_dataProcCounter(0), m_dataSentCounter(0), m_procEntry(0)
    {
        //add code here
    }

    //清理当前对象所占用的相关资源。
    ~WRWorkTcpCommServerDataSource(void)
    {
        m_status = false;
        m_source->removeHandler(this);
        if (m_dataBuffers)
        {
            m_dataBuffers->release();
        }
    }

    //当数据启动后调用此方法。
    void onStarted(const void* args)
    {
        if (m_status)
        {
            int argsLen = m_argsWriter.count(*(const TArgs*)args);
            int fillLen = 0;//填充长度
            {
                int remain = argsLen % 8;
                if (remain > 0) fillLen = 8 - remain;
            }

            StatProcEntry* entry;
            if (affair())
            {
                char* p;
                int cmdLen = 40 + argsLen + fillLen;
                entry = new StatProcEntry(this, cmdLen);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                p += 4;
                WRBytes::write((WR32s)(cmdLen), p);//帧长度
                p += 4;
                WRBytes::write((WR16u)(object()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectAffair, p);//类型（工作事务）
                p += 2;
                WRBytes::write((WR32s)(affair()->id()), p);
                p += 4;
                WRBytes::write((WR16u)(affair()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkAffairIITreat, p);//类型（接口处理）
                p += 2;
                WRBytes::write((WR16u)(index()), p);//接口索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::DataStarted, p);//数据启动
                p += 6;
                WRBytes::write((WR32s)(argsLen), p);//参数长度
                p += 4;
                m_argsWriter.write(*(const TArgs*)args, (unsigned char*)p);//参数数据
                p += argsLen;
                p += fillLen; ;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
                p += 4;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾
            }
            else
            {
                char* p;
                int cmdLen = 32 + argsLen + fillLen;
                entry = new StatProcEntry(this, cmdLen);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                p += 4;
                WRBytes::write((WR32s)(cmdLen), p);//帧长度
                p += 4;
                WRBytes::write((WR16u)(object()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectIITreat, p);//类型（接口处理）
                p += 2;
                WRBytes::write((WR16u)(index()), p);//接口索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::DataStarted, p);//数据启动
                p += 6;
                WRBytes::write((WR32s)(argsLen), p);//参数长度
                p += 4;
                m_argsWriter.write(*(const TArgs*)args, (unsigned char*)p);//参数数据
                p += argsLen;
                p += fillLen; ;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
                p += 4;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾
            }
            beginProcess(entry);
        }
    }
    //当数据改变后调用此方法。
    void onChanged(const void* args)
    {
        if (m_status)
        {
            int argsLen = m_argsWriter.count(*(const TArgs*)args);
            int fillLen = 0;//填充长度
            {
                int remain = argsLen % 8;
                if (remain > 0) fillLen = 8 - remain;
            }

            StatProcEntry* entry;
            if (affair())
            {
                char* p;
                int cmdLen = 40 + argsLen + fillLen;
                entry = new StatProcEntry(this, cmdLen);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                p += 4;
                WRBytes::write((WR32s)(cmdLen), p);//帧长度
                p += 4;
                WRBytes::write((WR16u)(object()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectAffair, p);//类型（工作事务）
                p += 2;
                WRBytes::write((WR32s)(affair()->id()), p);
                p += 4;
                WRBytes::write((WR16u)(affair()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkAffairIITreat, p);//类型（接口处理）
                p += 2;
                WRBytes::write((WR16u)(index()), p);//接口索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::DataChanged, p);//数据改变
                p += 6;
                WRBytes::write((WR32s)(argsLen), p);//参数长度
                p += 4;
                m_argsWriter.write(*(const TArgs*)args, (unsigned char*)p);//参数数据
                p += argsLen;
                p += fillLen; ;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
                p += 4;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾
            }
            else
            {
                char* p;
                int cmdLen = 32 + argsLen + fillLen;
                entry = new StatProcEntry(this, cmdLen);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                p += 4;
                WRBytes::write((WR32s)(cmdLen), p);//帧长度
                p += 4;
                WRBytes::write((WR16u)(object()->index()), p);//工作对象索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectIITreat, p);//类型（接口处理）
                p += 2;
                WRBytes::write((WR16u)(index()), p);//接口索引
                p += 2;
                WRBytes::write((WR16u)WRWorkTcpComm::DataChanged, p);//数据改变
                p += 6;
                WRBytes::write((WR32s)(argsLen), p);//参数长度
                p += 4;
                m_argsWriter.write(*(const TArgs*)args, (unsigned char*)p);//参数数据
                p += argsLen;
                p += fillLen; ;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p);//版本
                p += 4;
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p);//帧尾
            }
            beginProcess(entry);
        }
    }
    //当 数据改变后调用此方法。
    void doProcess(const void* value)
    {
        WRBuffer<char>* buffer = 0;
        if (m_status)
        {
            ++m_dataProcCounter;
            buffer = m_dataBuffers->request();
        }
        if (buffer)
        {
            ++m_dataSentCounter;

            ProcEntry* entry;
            if (affair())
            {
                entry = new AffairDataProcEntry(this, buffer, *(const TData*)value);
            }
            else
            {
                entry = new ObjectDataProcEntry(this, buffer, *(const TData*)value);
            }
            beginProcess(entry);
        }
    }
    //当数据停止后调用此方法。
    void onStopped(void)
    {
        if (m_status)
        {
            StatProcEntry* entry;
            if (affair())
            {
                char* p;
                entry = new StatProcEntry(this, 32);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                WRBytes::write((WR32s)(32), p + 4);//帧长度
                WRBytes::write((WR16u)(object()->index()), p + 8);//工作对象索引
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectAffair, p + 10);//类型（工作事务）
                WRBytes::write((WR32s)(affair()->id()), p + 12);
                WRBytes::write((WR16u)(affair()->index()), p + 16);//工作对象索引
                WRBytes::write((WR16u)WRWorkTcpComm::WorkAffairIITreat, p + 18);//类型（接口处理）
                WRBytes::write((WR16u)(index()), p + 20);//接口索引
                WRBytes::write((WR16u)WRWorkTcpComm::DataStopped, p + 22);//数据停止
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p + 24);//版本
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p + 28);//帧尾
            }
            else
            {
                char* p;
                entry = new StatProcEntry(this, 24);
                entry->buffer(&p);

                WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                WRBytes::write((WR32s)(24), p + 4);//帧长度
                WRBytes::write((WR16u)(object()->index()), p + 8);//工作对象索引
                WRBytes::write((WR16u)WRWorkTcpComm::WorkObjectIITreat, p + 10);//类型（接口处理）
                WRBytes::write((WR16u)(index()), p + 12);//接口索引
                WRBytes::write((WR16u)WRWorkTcpComm::DataStopped, p + 14);//数据停止
                WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p + 16);//版本
                WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p + 20);//帧尾
            }
            beginProcess(entry);
        }
    }

    //执行关闭处理。
    void shutdown(void)
    {
        m_status = false;
        m_source->removeHandler(this);
    }

    //处理数据。
    void procData(const unsigned char* buf, int len)
    {
        WR16u cmdType = WRBytes::read<WR16u>(buf);;
        buf += 2;
        len -= 2;
        switch(cmdType)
        {
        case WRWorkTcpComm::DataStarted:
        {
            m_bufSize = WRBytes::read<WR32s>(buf);//缓存数量
            m_pkgSize = WRBytes::read<WR32s>(buf + 4);//每包大小
            {
                WR32s remain = m_pkgSize % 8;
                if (remain > 0) m_pkgSize += 8 - remain;
            }
            if (m_dataBuffers)
            {
                m_dataBuffers->release();
                m_dataBuffers = 0;
            }
            m_dataBuffers = new WRBuffers<char>(m_bufSize);
            m_source->addHandler(this);
        }
            break;
        case WRWorkTcpComm::DataStopped:
        {
            m_source->removeHandler(this);
            if (m_dataBuffers)
            {
                m_dataBuffers->release();
                m_dataBuffers = 0;
            }
        }
            break;
        default:
            break;
        }
    }
};

//表示工作TCP通讯服务器数据激活器。
template <class TArgs, class TData, class TArgsWriter, class TDataWriter, class TUuidValue, class TNameValue>
class WRWorkTcpCommServerDataSourceActivator : public WRWorkTcpCommServerInterfaceActivator
{
public:
    //初始化工作TCP通讯服务器数据激活器。
    WRWorkTcpCommServerDataSourceActivator(void)
    {
        TUuidValue uuid;
        WRWorkTcpComm::enrollServerInterface(uuid(), this);
    }
    //释放当前对象所占用的相关资源。
    ~WRWorkTcpCommServerDataSourceActivator(void)
    {
        TUuidValue uuid;
        WRWorkTcpComm::cancelServerInterface(uuid());
    }

    //创建接口实例。
    WRWorkTcpCommServerInterface* create(int index, WRWorkTcpCommServerObject* target, void* reference)
    {
        TUuidValue uuid;
        TNameValue name;
        return new WRWorkTcpCommServerDataSource<TArgs, TData, TArgsWriter, TDataWriter>(uuid(), name(), index, target, (WRDataSource*)reference);
    }
    //创建接口实例。
    WRWorkTcpCommServerInterface* create(int index, WRWorkTcpCommServerAffair* target, void* reference)
    {
        TUuidValue uuid;
        TNameValue name;
        return new WRWorkTcpCommServerDataSource<TArgs, TData, TArgsWriter, TDataWriter>(uuid(), name(), index, target, (WRDataSource*)reference);
    }
};

#endif // WRWORKTCPCOMMSERVERDATASOURCE
